﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.IO;

namespace Infrastructure.Aspect.Services.Serialization
{
    public class DataContractSerializerService: ISerializationService
    {
        public byte[] Serialization(object target)
        {
            var dc = new DataContractSerializer(target.GetType());
            using (var ms = new MemoryStream())
            {
                dc.WriteObject(ms, target);
                return ms.ToArray();
            }           
        }

        public T DesSerialization<T>(byte[] target)
        {
            var dc = new DataContractSerializer(typeof(T));
            using (var ms = new MemoryStream(target))
            {
                var result = (T)dc.ReadObject(ms);
                return result;
            }           
        }
    }
}
